#include "propdata.h"
#include <QDebug>

PropData::PropData(QObject *parent) :
    QObject(parent)
{
    propCoords.resize(4);
    localPaths = true;
    //audMain = new Tuple<QString, bool>("", PROP_LOCAL_PATH);
    audMainScript = new Tuple<QString, bool>("", PROP_LOCAL_PATH);
    //audMainScriptFile = new Tuple<QString, bool>("", PROP_LOCAL_PATH);
    propLoc = PROP_IS_LOCAL;
    propStatus = PROP_NOT_SAVED;
}

PropData::PropData(PropData *other, QObject *parent) :
    QObject(parent)
{
    this->localPaths = other->localPaths;

    this->propName = other->propName;
    //this->audMain->first = other->audMain->first;
    //this->audMain->second = other->audMain->second;
    this->audMainScript->first = other->audMainScript->first;
    this->audMainScript->second = other->audMainScript->second;
    this->propCoords = std::vector<Coordinate>(other->propCoords);
    this->aud = std::vector<Tuple<QString, QString>* >(other->aud);
    this->vid = std::vector<Tuple<QString, QString>* >(other->vid);
    this->pic = std::vector<Tuple<QString, QString>* >(other->pic);
    //this->aud = std::vector<Tuple<QString, bool>* >(other->aud);
    //this->vid = std::vector<Tuple<QString, bool>* >(other->vid);
    //this->pic = std::vector<Tuple<QString, bool>* >(other->pic);
    this->propLoc = other->propLoc;
    this->propStatus = other->propStatus;
}

/**
  Create a prop from some propdata.
  fromDevice - true if propdata is from the device (paths are not local) (default)
  */
PropData::PropData(QString pName,
                   std::vector<Coordinate> pCoords,
                   QString aMain,
                   QString aMScript,
                   QString aMScriptF,
                   std::vector<QString> a,
                   std::vector<QString> v,
                   std::vector<QString> p,
                   bool fromDevice)
{
    propCoords.resize(4);
    localPaths = false;
    propName = pName;
    propCoords = pCoords;
    //audMain = new Tuple<QString, bool>(aMain, fromDevice);
    audMainScript = new Tuple<QString, bool>(aMScript, fromDevice);
    //audMainScriptFile = new Tuple<QString, bool>(aMScriptF, fromDevice);
    if (fromDevice)
    {
        audMain = "";
        audMainDevice = aMain;
        audMainScriptFile = "";
        audMainScriptFileDevice = aMScriptF;
        for (unsigned int k = 0; k < a.size(); k++)
        {
            aud.push_back(new Tuple<QString, QString>("", a[k]));
        }
        for (unsigned int k = 0; k < v.size(); k++)
        {
            vid.push_back(new Tuple<QString, QString>("", v[k]));
        }
        for (unsigned int k = 0; k < p.size(); k++)
        {
            pic.push_back(new Tuple<QString, QString>("", p[k]));
        }
        propLoc = PROP_IS_DEVICE;
        propStatus = PROP_SAVED;
    }
    else
    {
        audMain = aMain;
        audMainDevice = "";
        audMainScriptFile = aMScriptF;
        audMainScriptFileDevice = "";
        for (unsigned int k = 0; k < a.size(); k++)
        {
            aud.push_back(new Tuple<QString, QString>(a[k], ""));
        }
        for (unsigned int k = 0; k < v.size(); k++)
        {
            vid.push_back(new Tuple<QString, QString>(v[k], ""));
        }
        for (unsigned int k = 0; k < p.size(); k++)
        {
            pic.push_back(new Tuple<QString, QString>(p[k], ""));
        }
        propLoc = PROP_IS_LOCAL;
        propStatus = PROP_NOT_SAVED;
    }
    /*for (unsigned int k = 0; k < a.size(); k++)
    {
        aud.push_back(new Tuple<QString, bool>(a[k], fromDevice));
    }
    for (unsigned int k = 0; k < v.size(); k++)
    {
        vid.push_back(new Tuple<QString, bool>(v[k], fromDevice));
    }
    for (unsigned int k = 0; k < p.size(); k++)
    {
        pic.push_back(new Tuple<QString, bool>(p[k], fromDevice));
    }*/
}

PropData::~PropData()
{
}

QString PropData::getPropName()
{
    return propName;
}

void PropData::changePropName(QString pName)
{
    if (propName != pName)
    {
        updateAllPaths(propName, pName);
        propName = pName;
        localPaths = true;
        if (propLoc == PROP_IS_DEVICE)
            propLoc = PROP_IS_MIXED;
        propStatus = PROP_NOT_SAVED;
    }
}

void PropData::updateAllPaths(QString oldPName, QString newPName)
{
    if (audMainDevice != "")
    {
        audMainDevice = updatePath(audMainDevice, oldPName, newPName);
    }
    if (audMainScriptFileDevice != "")
    {
        audMainScriptFileDevice = updatePath(audMainScriptFileDevice, oldPName, newPName);
    }
    for (unsigned int k = 0; k < aud.size(); k++)
    {
        if (aud[k]->second != "")
        {
            aud[k]->second = updatePath(aud[k]->second, oldPName, newPName);
        }
    }
    for (unsigned int k = 0; k < vid.size(); k++)
    {
        if (vid[k]->second != "")
        {
            vid[k]->second = updatePath(vid[k]->second, oldPName, newPName);
        }
    }
    for (unsigned int k = 0; k < pic.size(); k++)
    {
        if (pic[k]->second != "")
        {
            pic[k]->second = updatePath(pic[k]->second, oldPName, newPName);
        }
    }
}

QString PropData::updatePath(QString old, QString oldPName, QString newPName)
{
    int index = old.indexOf(oldPName);
    old.replace(index, oldPName.size(), newPName);
    return old;
}

/*Tuple<QString, bool>* const& PropData::getAudMain() const
{
    return audMain;
}

Tuple<QString, bool>* const& PropData::getAudMainScript() const
{
    return audMainScript;
}

Tuple<QString, bool>* const& PropData::getAudMainScriptFile() const
{
    return audMainScriptFile;
}

std::vector<Tuple<QString, bool>* > const& PropData::getAud() const
{
    return aud;
}

std::vector<Tuple<QString, bool>* > const& PropData::getVid() const
{
    return vid;
}

std::vector<Tuple<QString, bool>* > const& PropData::getPic() const
{
    return pic;
}*/

void PropData::setAudMain(QString aMain)
{
    if (audMain != aMain)
    {
        audMain = aMain;
        audMainDevice = "";
        localPaths = true;
        if (propLoc == PROP_IS_DEVICE)
            propLoc = PROP_IS_MIXED;
        propStatus = PROP_NOT_SAVED;
    }
}

void PropData::setAudMainScript(QString script, bool fromXML)
{
    if (fromXML)
    {
        audMainScript->first = script;
        audMainScript->second = PROP_DEVICE_PATH;
        return;
    }
    if (audMainScript->first != script)
    {
        audMainScript->first = script;
        audMainScript->second = PROP_LOCAL_PATH;
        //audMainScriptFile->second = PROP_LOCAL_PATH;
        localPaths = true;
        if (propLoc == PROP_IS_DEVICE)
            propLoc = PROP_IS_MIXED;
        propStatus = PROP_NOT_SAVED;
    }
}

void PropData::setAudMainScriptFile(QString scrFile)
{
    if (audMainScriptFile != scrFile)
    {
        audMainScriptFile = scrFile;
        audMainScriptFileDevice = "";
        localPaths = true;
        if (propLoc == PROP_IS_DEVICE)
            propLoc = PROP_IS_MIXED;
        propStatus = PROP_NOT_SAVED;
    }
}

void PropData::addAud(std::vector<QString> a)
{
    bool found = false;
    std::vector<Tuple<QString, QString>* > tmp;
    for (unsigned int k = 0; k < a.size(); k++)
    {
        found = false;
        for (unsigned int j = 0; j < aud.size(); j++)
        {
            if (a[k] == aud[j]->first || a[k] == aud[j]->second)
            {
                found = true;
                tmp.push_back(aud[j]);
                break;
            }
        }
        if (!found)
        {
            tmp.push_back(new Tuple<QString, QString>(a[k], ""));
            localPaths = true;
            if (propLoc == PROP_IS_DEVICE)
                propLoc = PROP_IS_MIXED;
            propStatus = PROP_NOT_SAVED;
        }
    }
    aud = std::vector<Tuple<QString, QString>* >(tmp);
}

void PropData::addVid(std::vector<QString> v)
{
    bool found = false;
    std::vector<Tuple<QString, QString>* > tmp;
    for (unsigned int k = 0; k < v.size(); k++)
    {
        found = false;
        for (unsigned int j = 0; j < vid.size(); j++)
        {
            if (v[k] == vid[j]->first || v[k] == vid[j]->second)
            {
                found = true;
                tmp.push_back(vid[j]);
                break;
            }
        }
        if (!found)
        {
            tmp.push_back(new Tuple<QString, QString>(v[k], ""));
            localPaths = true;
            if (propLoc == PROP_IS_DEVICE)
                propLoc = PROP_IS_MIXED;
            propStatus = PROP_NOT_SAVED;
        }
    }
    vid = std::vector<Tuple<QString, QString>* >(tmp);
}

void PropData::addPic(std::vector<QString> p)
{
    bool found = false;
    std::vector<Tuple<QString, QString>* > tmp;
    for (unsigned int k = 0; k < p.size(); k++)
    {
        found = false;
        for (unsigned int j = 0; j < pic.size(); j++)
        {
            if (p[k] == pic[j]->first || p[k] == pic[j]->second)
            {
                found = true;
                tmp.push_back(pic[j]);
                break;
            }
        }
        if (!found)
        {
            tmp.push_back(new Tuple<QString, QString>(p[k], ""));
            localPaths = true;
            if (propLoc == PROP_IS_DEVICE)
                propLoc = PROP_IS_MIXED;
            propStatus = PROP_NOT_SAVED;
        }
    }
    pic = std::vector<Tuple<QString, QString>* >(tmp);
}
